package com.sniff.atp.modules.test.controller.ops;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckbs.user.client.dto.resp.ManageLoginRespDTO;
import com.ckbs.user.client.utils.JwtUtil;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.OpsMembershipChange;
import com.sniff.atp.modules.test.model.OpsMembershipInfo;
import com.sniff.atp.modules.test.service.OpsMembershipChangeService;
import com.sniff.atp.utils.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 赠送会员 前端控制器
 * </p>
 *
 * @author macro
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/ops/opsMembershipChange")
@Api(value = "OPS赠送会员", tags = "OPS赠送会员")
public class OpsMembershipChangeController {
    @Autowired
    private OpsMembershipChangeService opsMembershipChangeService;

    @ApiOperation("新增赠送会员数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(ServletRequest servletRequest,@RequestBody OpsMembershipChange opsMembershipChange) {


//        获取用户基本信息
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        opsMembershipChange.setOperatePerson(manageDTO.getNick());

        boolean success = opsMembershipChangeService.create(opsMembershipChange);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改赠送会员数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody OpsMembershipChange opsMembershipChange) {

        boolean success = opsMembershipChangeService.update(opsMembershipChange.getOpsMembershipChangeId(),opsMembershipChange);
        if (success) {
            return CommonResult.success(null);


        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取需求详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OpsMembershipChange> getItem(@PathVariable Long id) {
        OpsMembershipChange opsMembershipChange = opsMembershipChangeService.getById(id);
        return CommonResult.success(opsMembershipChange);
    }

    @ApiOperation("根据ID删除问题列表")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = opsMembershipChangeService.removeById(id);

        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<OpsMembershipChange>> list(@RequestBody OpsMembershipChange opsMembershipChange) {
        if(opsMembershipChange.getPageSize() == null){
            opsMembershipChange.setPageSize(10);
        }
        if(opsMembershipChange.getPageNum() == null){
            opsMembershipChange.setPageNum(1);
        }
        Page<OpsMembershipChange> menuList = opsMembershipChangeService.list(opsMembershipChange, opsMembershipChange.getPageSize(), opsMembershipChange.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("查询用户的基本信息")
    @RequestMapping(value = "/customerInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OpsMembershipChange> customerInfo(ServletRequest servletRequest,@RequestBody OpsMembershipChange opsMembershipChange) {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        opsMembershipChange.setOperatePerson(manageDTO.getNick());

        //客户id，赠送会员时间，会员金额必输
        if(opsMembershipChange.getCustomerId() == null || opsMembershipChange.getDay() == null || opsMembershipChange.getMembershipFee() ==null){
            return CommonResult.failed("客户id，赠送会员时间，会员金额必输!!");
        }

        OpsMembershipChange opsMembershipChange2 = opsMembershipChangeService.getCustomerInfo(opsMembershipChange);

        if(opsMembershipChange2.getCanSendFlag()){
            return CommonResult.success(opsMembershipChange2);
        }else{
            return CommonResult.failed("会员的站点和赠送的站点不一致，不支持赠送");
        }
    }

    @ApiOperation("根据客户id获取会员信息")
    @RequestMapping(value = "/getMembershipInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OpsMembershipChange> getMembershipInfo(ServletRequest servletRequest, @RequestBody OpsMembershipChange opsMembershipChange) {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        opsMembershipChange.setOperatePerson(manageDTO.getNick());

        //客户id，赠送会员时间，会员金额必输
        if(opsMembershipChange.getCustomerId() == null ){
            return CommonResult.failed("客户id必输!!");
        }

        OpsMembershipChange opsMembershipChange2 = opsMembershipChangeService.getMembershipInfo(opsMembershipChange);
        return CommonResult.success(opsMembershipChange2);
    }

}

